﻿using System.Text;
using roysland.net.xcom.core.Helpers;

namespace roysland.net.xcom.core.Models
{
    public class ObDataRecord
    {
        private string _objectName;
        #region Constructor
        public ObDataRecord(byte[] buffer)
        {
            RecordBuffer = buffer;
            GetObjectName();
        }

        public ObDataRecord(byte[] buffer, int recordNumber)
        {
            RecordBuffer = buffer;
            RecordNumber = recordNumber;
            GetObjectName();
        }
        #endregion

        public string GetObjectName()
        {
            byte[] buffer = ByteHelper.GetByteSegment(0, 19, RecordBuffer);

            var enc = new ASCIIEncoding();
            _objectName = enc.GetString(buffer);
            _objectName = _objectName.Replace('\0', ' ');
            _objectName = _objectName.Replace('\n', ' ');
            _objectName = _objectName.Replace('\r', ' ');
            _objectName.Trim();
            return _objectName;
        }

        #region Properties
        public int RecordNumber { get; set; }
        public byte[] RecordBuffer { get; set; }


        public string ObjectName
        {
            get { return _objectName; }
        }

        public int BigObsIndex
        {
            get { return ByteHelper.ByteToInt(20, RecordBuffer); }
        }

        public int Damage
        {
            get { return ByteHelper.ByteToInt(22, RecordBuffer); }
            set { RecordBuffer[22] = ByteHelper.IntToByte(value); }
        }

        public int Weight
        {
            get { return ByteHelper.ByteToInt(43, RecordBuffer); }
            set { RecordBuffer[43] = ByteHelper.IntToByte(value); }
        }

        public int GrenadeType
        {
            get { return ByteHelper.ByteToInt(44, RecordBuffer); }
            set { RecordBuffer[44] = ByteHelper.IntToByte(value); }
        }

        public int AmmoDamageType
        {
            get { return ByteHelper.ByteToInt(31, RecordBuffer); }
            set { RecordBuffer[31] = ByteHelper.IntToByte(value); }
        }

        public int AccuracyAutoShot
        {
            get { return ByteHelper.ByteToInt(32, RecordBuffer); }
            set { RecordBuffer[32] = ByteHelper.IntToByte(value); }
        }

        public int AccuracySnapShot
        {
            get { return ByteHelper.ByteToInt(33, RecordBuffer); }
            set { RecordBuffer[33] = ByteHelper.IntToByte(value); }
        }

        public int AccuracyAimedShot
        {
            get { return ByteHelper.ByteToInt(34, RecordBuffer); }
            set { RecordBuffer[34] = ByteHelper.IntToByte(value); }
        }

        public int TimeUnitsAutoShot
        {
            get { return ByteHelper.ByteToInt(35, RecordBuffer); }
            set { RecordBuffer[35] = ByteHelper.IntToByte(value); }
        }

        public int TimeUnitsSnapShot
        {
            get { return ByteHelper.ByteToInt(36, RecordBuffer); }
            set { RecordBuffer[36] = ByteHelper.IntToByte(value); }
        }

        public int TimeUnitsAimedShot
        {
            get { return ByteHelper.ByteToInt(37, RecordBuffer); }
            set { RecordBuffer[37] = ByteHelper.IntToByte(value); }
        }


        #endregion
    }
}
